<template>
  <div class="login-page">
    <div class="login-box">
      <div class="avatar-box">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt="" />
      </div>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submit"> 登录 </el-button>
          <el-button type="info" @click="reset">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/e-commerce";
import { Message } from "element-ui";

export default {
  name: "LoginIndex",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名" },
          { min: 3, max: 10, message: "长度需要在3到10位之间" },
        ],
        password: [
          { required: true, message: "请输入密码" },
          { pattern: /^\w{6,15}$/, message: "长度需要在6到15位之间" },
        ],
      },
    };
  },
  methods: {
    async submit() {
      try {
        // 校验表单
        await this.$refs.form.validate();
        const res = await login(this.form);
        console.log(res.data.data.token);
        this.$store.commit("user/updateToken", res.data.data.token);
        this.$router.push("/");
        Message.success("登录成功");
      } catch (err) {
        console.log(err);
      }
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #2b4b6b;
  .login-box {
    background-color: white;
    width: 450px;
    height: 300px;
    position: relative;
    .el-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .btn {
        display: flex;
        justify-content: flex-end;
      }
    }
    .avatar-box {
      width: 130px;
      height: 130px;
      margin: 0 auto;
      border-radius: 50%;
      transform: translateY(-50%);
      border: 4px solid #fff;
      background: #eeeeee;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
